/**
 * 工具类
 */
import CryptoJS from 'crypto-js'
import moment from "moment";
import {get, post} from "./axios";
import {Message} from "element-ui";

const api = {
    selectorUrl: '/v1/api/selector/:action/:selector',
    statusUrl: '/v1/api/:work/:action/:id',
    watchGetUrl: '/v1/api/getWatch/:api_name'
}

export const constPool = {
    TOKEN: 'access_token',
    NICKNAME: 'nick_name',
    ACCOUNT: 'account'
}

export const options = {
    method: [
        {value: 'GET', label: 'GET'},
        {value: 'POST', label: 'POST'},
        {value: 'PUT', label: 'PUT'},
        {value: 'PATCH', label: 'PATCH', disabled: true},
        {value: 'DELETE', label: 'DELETE'},
        {value: 'COPY', label: 'COPY', disabled: true},
        {value: 'HEAD', label: 'HEAD', disabled: true},
        {value: 'OPTIONS', label: 'OPTIONS', disabled: true},
        {value: 'LINK', label: 'LINK', disabled: true},
        {value: 'UNLINK', label: 'UNLINK', disabled: true},
        {value: 'PURGE', label: 'PURGE', disabled: true},
        {value: 'VIEW', label: 'VIEW', disabled: true}
    ],
    content_type: [
        {value: "application/json", label: "application/json"},
        {value: "application/x-www-form-urlencoded", label: "application/x-www-form-urlencoded"},
        {value: "application/xml", label: "application/xml", disabled: true},
        {value: "", label: "none"}
    ],
    argument_type: [
        {value: 'path', label: 'path'},
        {value: 'param', label: 'param'},
        {value: 'header', label: 'header'},
        {value: 'body', label: 'body'},
        {value: 'auto', label: 'auto'}
    ],
    argument_opinion: [
        {value: 'empty', label: ''},
        {value: 'required', label: 'required'},
        {value: 'static', label: 'static'}
    ],
    value_type: [
        {value: '', label: "value"},
        {value: 'string', label: 'string'},
        {value: 'float', label: 'number'},
        {value: 'map', label: '{}', disabled: true},
        {value: 'slice', label: '[]', disabled: true},
    ],
    net: [
        {value: 'https://', label: 'https://'},
        {value: 'http://', label: 'http://'},
        {value: 'ws://', label: 'ws://', disabled: true},
        {value: 'wss://', label: 'wss://', disabled: true}
    ]
}


export default {
    //加密
    encrypt(word, keyStr) {
        keyStr = keyStr ? keyStr : 'mRzEAlhF0QTE4KfW';
        let key = CryptoJS.enc.Utf8.parse(keyStr);
        let srcs = CryptoJS.enc.Utf8.parse(word);
        let encrypted = CryptoJS.AES.encrypt(srcs, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
        return encrypted.toString();
    },
    //解密
    decrypt(word, keyStr) {
        keyStr = keyStr ? keyStr : 'mRzEAlhF0QTE4KfW';
        let key = CryptoJS.enc.Utf8.parse(keyStr);
        let decrypt = CryptoJS.AES.decrypt(word, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
        return CryptoJS.enc.Utf8.stringify(decrypt).toString();
    },
    // isEmpty
    isEmpty(...params) {
        for (let i = 0; i < params.length; i++) {
            if (params[i] === null || params[i] === undefined || params[i] === '') {
                return true
            }
        }
        return false
    },
    dateFormat(row, column) {
        let date = row[column.property];
        date = date * 1000
        if (date === undefined) {
            return ''
        }
        return moment(date).format("MM-DD HH:mm:ss YYYY")
    },
    selector(action, select) {
        let url = api.selectorUrl.replace(":action", action).replace(":selector", select)
        return post(url, {}, "", false, false)
    },
    updateStatus(work, action, id) {
        let statusUrl = api.statusUrl.replace(":work", work).replace(":action", action).replace(":id", id)
        if (this.isEmpty(work, action, id)) {
            Message({
                message: '请求参数缺失，请检查',
                type: "error"
            })
        } else {
            post(statusUrl, {}, {}, true, false).then(response => {
                if (response.data.code === 0) {
                    Message({
                        message: "状态已更新",
                        type: "success"
                    })
                } else {
                    Message({
                        message: response.data.msg,
                        type: "warning",
                    })
                }
            })
        }
    },
    getWatch(apiName) {
        let getWatchUrl = api.watchGetUrl.replace(":api_name", apiName)
        return get(getWatchUrl, "", {}, false)
    },
    constPool,
    options
}